package cn.edu.cuc.yjsy.admin.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import cn.edu.cuc.yjsy.database.DatabasePackage;
import cn.edu.cuc.yjsy.report.Report;
import cn.edu.cuc.yjsy.report.ReportFactory;
import cn.edu.cuc.yjsy.report.ReportFolder;
import cn.edu.cuc.yjsy.report.ReportModelManager;
import cn.edu.cuc.yjsy.report.ReportRoot;

public class ReportTest {
	public static void main(String[] args) {
//		generateReport();
		loadReport();
	}
	
	private static void loadReport(){
		File f = new File("V:\\workspace\\AdminSystem\\cn.edu.cuc.yjsy.admin\\report.xml");
		ReportModelManager manager = new ReportModelManager();
		try {
			manager.load(new Path(f.getCanonicalPath()));
			ReportRoot root = manager.getModel();
			root.getFolders();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void generateReport() {
		try {
			// Create a resource set to hold the resources.
			//
			ResourceSet resourceSet = new ResourceSetImpl();
			
			// Register the appropriate resource factory to handle all file extentions.
			//
			resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
				(Resource.Factory.Registry.DEFAULT_EXTENSION, 
				 new XMIResourceFactoryImpl());

			// Register the package to ensure it is available during loading.
			//
			resourceSet.getPackageRegistry().put
				(DatabasePackage.eNS_URI, 
				 DatabasePackage.eINSTANCE);
			Resource resource = resourceSet.createResource(URI.createURI("http:///My.database"));

			ReportFactory factory = ReportFactory.eINSTANCE;
			ReportRoot root = factory.createReportRoot();
			root.setName("研究生院");
			ReportFolder folder = factory.createReportFolder();
			folder.setName("研究生院学生管理报表");
			Report item = factory.createReport();
			item.setName("全部学生");
			item.setComment("查询全部学生");
			item.setUrl("http://localhost/all.rpdesign");
			folder.addItem(item);
			item = factory.createReport();
			item.setName("按年级查询");
			item.setComment("查询结果按年级分组");
			item.setUrl("http://localhost/allByGrade.rpdesign");
			folder.addItem(item);
			root.addFolder(folder);
			
			resource.getContents().add(root);
			Map options = new HashMap();
			options.put(XMIResource.OPTION_DECLARE_XML, Boolean.TRUE);
			options.put(XMIResource.OPTION_ENCODING, "UTF-8");
	    	File file = new File("report.xml");
			resource.save(new FileOutputStream(file), options);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
